let questions = [];
window.onload = function () {
    const queryString = window.location.search;
    const urlParams = new URLSearchParams(queryString);
    const questionId = urlParams.get('id');
    console.log('题目ID:', questionId);
    if (questionId) {

        fetch('/correct/getwronglist', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({ id: questionId })

        })
            .then(response => {
                if (!response.ok) {
                    throw new Error('Failed to fetch question details');
                }
                return response.json(); // 如果后端返回的是JSON数据
            })
            .then(data => {
                console.log('success:', data);
                questions = data;
                console.log(data);
                const questionsContainer = document.getElementById('questions');
                questions.forEach((item, index) => {
                    const questionDiv = document.createElement('div');
                    questionDiv.classList.add('question');
                    if (item.type === "Objective") {
                        let optionsHtml = "";
                        item.options.forEach((option, i) => {
                            optionsHtml += `<input type="radio" id="option${index}-${i}" name="question${index}" value="${option}"> <label for="option${index}-${i}">${option}</label><br>`;
                        });
                        questionDiv.innerHTML = `<p>${item.question}</p>${optionsHtml}`;
                        const userAnswer = item.answer;
                        const correctAnswer = item.correct;
                        const answerDisplay = document.createElement('div');
                        answerDisplay.textContent = `你的答案: ${userAnswer} | 正确答案: ${correctAnswer}`;
                        questionDiv.appendChild(answerDisplay);
                    }
                    else if (item.type === "Subjective") {
                        let questionHtml = `<p>${item.question}</p>`;
                        let correctAnswerHtml = `<p >你的答案: ${item.answer}</p>`
                        questionDiv.innerHTML = questionHtml + correctAnswerHtml;
                    }
                    questionsContainer.appendChild(questionDiv);
                });
            })
            .catch(error => {
                console.error('Error fetching question details:', error);
            });
    } else {
        console.log('No question ID provided in the URL.');
    }
}


